<?php

/**
 * 后台公共文件 
 * @file   Common.php  
 * @date   2016-8-24 18:28:34 
 * @author Zhenxun Du<5552123@qq.com>  
 * @version    SVN:$Id:$ 
 */

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Controller;

class Common extends Controller {

    public function _initialize() {
        if(!session('username')){
            $this->error('请登录');
        }
        if(!$this->_checkAuthor()){
            $this->error('您无权限操作');
        }
        $username = session('username');
        $role_id = session('role_id');
        $left_lists = Db::table('ticket_node')
                    ->alias('n')
                    ->join('ticket_role_node r', 'n.id=r.node_id')
                    ->where('role_id',$role_id)
                    ->select();
        $this->assign("left_lists", $left_lists);
        $this->assign("show_username", $username);
        $this->_addLog();
    }
    private function _addLog() {
        $data = array();
        $data['querystring'] = request()->query()?'?'.request()->query():'';
        $data['m'] = request()->module();
        $data['c'] = request()->controller();
        $data['a'] = request()->action();
        $data['ip'] = ip2long(request()->ip());
	    $data['time'] = time();
        $arr = array('Index/index','Log/index','Menu/index');
        db('admin_log')->insert($data);
    }

    private function _checkAuthor(){
        $c = strtolower(request()->controller());
        $a = strtolower(request()->action());
        $role_id = session('role_id');
        if($role_id == 1){
            if($a=='check_ticket'){
                return false;
            }
        }
        else if($role_id == 2){
            if($a=='start_ticket'){
                return false;
            }
        }
        return true;
    }

}